<?php
    header("Access-Control-Allow-Origin:*");
    header("Content-type:text/html;charset=utf-8");

    //连接数据库
    require "./connect.php";

    # 获取前端传入的数据:
    $username = @$_POST["username"];
    $password = @$_POST["password"];
    $tel = @$_POST["tel"];

    //数据库sql语句 : 从表中获取数据
    // if($password == null && $tel == null){
        $select_SQL = "SELECT username FROM user";
        $select_res = mysql_query($select_SQL,$con);  //运行sql语句
        if($select_res){
            while($row = mysql_fetch_array($select_res)){
                if($row["username"] == $username){
                    $json_arr = array( "statu" => "error" , "stateCode" => "3", "errorType" => "该昵称已被他人使用");
                    die(json_encode($json_arr));
                }
            }
        }
            
    // }

    //当用户名 密码和手机号不为空的时候才会进行插入
    if($username != null && $password != null && $tel != null){
        //插入数据
        $insert_SQL = "INSERT INTO user (
            `username`,
            `password`,
            `phonenum`
        )
            VALUES 
        (
            '$username',
            '$password',
            '$tel'
        )";
        $res = mysql_query($insert_SQL,$con);

        if(!$res){
            //数据库插入错误
            $json_arr = array( "statu" => "error" , "stateCode" => "4" , "errorType" => "注册失败".mysql_error());
            die(json_encode($json_arr));
        }
    }
    $json_arr = array( "statu" => "success" , "stateCode" => "1");
    die(json_encode($json_arr));
?>